#include "mapmergeview.h"
#include "ui_mapmergeview.h"
#include <QFileDialog>
#include "commonhead.h"
#include "mapfileparse.h"
#include "CppSQLite3.h"
#include "mapdb.h"

#define IF_USE_DB 1

MapMergeView::MapMergeView(OneMap *oneMapIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MapMergeView)
{
    oneMap=oneMapIn;
    middleMap=new OneMap;
    ui->setupUi(this);
}

MapMergeView::~MapMergeView()
{
    delete ui;
}

void MapMergeView::showView_slot()
{
    show();
}

void MapMergeView::on_pushButton_browser_clicked()
{

    //定义文件对话框类
   QFileDialog *fileDialog = new QFileDialog(this);
   fileDialog->resize(400,500);
   //定义文件对话框标题
   fileDialog->setWindowTitle(tr("选择文件"));
   //设置默认文件路径
   QString configFile=D_MAP_FILE_PATH;

   fileDialog->setDirectory(configFile);
   //设置文件过滤器
   if(IF_USE_DB)
   {
        fileDialog->setNameFilter(tr("db(*.db)"));
   }
   else
   {
        fileDialog->setNameFilter(tr("json(*.mjson)"));
   }



   //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
   fileDialog->setFileMode(QFileDialog::ExistingFiles);
   //设置视图模式
   fileDialog->setViewMode(QFileDialog::Detail);
   //打印所有选择的文件的路径
   QStringList fileNames;
   if(fileDialog->exec())
   {
       fileNames = fileDialog->selectedFiles();
   }
    qDebug()<<fileNames;
    if(1==fileNames.size())
    {
        ui->lineEdit_newPathMap->setText(fileNames[0]);
    }
    else
    {
        ui->lineEdit_newPathMap->clear();
    }
}

void MapMergeView::on_pushButton_mergePathMap_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否合并路径地图?"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    ui->textEdit_result->clear();


    QString tmpNote="原地图版本:";
    tmpNote+=QString::number(oneMap->getVersion());
    ui->textEdit_result->append(tmpNote);


    QString filePath =ui->lineEdit_newPathMap->text();
    if(0==filePath.size())
    {
        QMessageBox::information(this,QStringLiteral("提示"),"文件路径为空");
        return ;
    }
    middleMap=oneMap->clone();
    if(IF_USE_DB)
    {
        CppSQLite3DB sqliteDb;
        try
        {
            sqliteDb.open(filePath.toLatin1().data());
        }
        catch (CppSQLite3Exception &e)
        {
           qDebug()<<"open map db,sqlite error";// {0}:{1};", e.errorCode(), e.errorMessage());

        }
        MapDB mapDb(oneMap,&sqliteDb);
        mapDb.load();

    }
    else
    {
        MapFileParse mapfileParser;
        mapfileParser.loadMapFromFile(true,filePath,oneMap);

    }



    tmpNote="导入地图版本:";
    tmpNote+=QString::number(oneMap->getVersion());
    ui->textEdit_result->append(tmpNote);

    std::list<MapPoint *> tmpPointList=oneMap->getPoints();

    foreach (auto point, tmpPointList)
    {
        MapPoint *tmpValidPoint=middleMap->getPointById(point->getId());
        if(nullptr==tmpValidPoint)
        {
            QString tmpNote="info:新增加点，id:";
            tmpNote+=QString::number(point->getId());
            ui->textEdit_result->append(tmpNote);
        }
        else
        {
            if(point->getPoseType() != tmpValidPoint->getPoseType())
            {
                QString tmpNote="warn:点位置类型不一致,坐标不会替换，id:";
                tmpNote+=QString::number(point->getId())+"新类型"+QString::number(point->getPoseType())
                        +"旧类型"+QString::number(tmpValidPoint->getPoseType());
                ui->textEdit_result->append(tmpNote);
            }
            else
            {
                if(E_POSITION_TYPE_COMMON==point->getPoseType() && ui->checkBox_rosPosition->isChecked())
                {


                        point->setX(tmpValidPoint->getX());
                        point->setY(tmpValidPoint->getY());
                        point->setRealX(tmpValidPoint->getRealX());
                        point->setRealY(tmpValidPoint->getRealY());
                        point->setRealA(tmpValidPoint->getRealA());
                        QString tmpNote="sucess:普通点坐标修改成功，id:";
                        tmpNote+=QString::number(point->getId());
                        ui->textEdit_result->append(tmpNote);

                }
                if(E_POSITION_TYPE_VMARK==point->getPoseType() && ui->checkBox_vmarkPosition->isChecked())
                {


                        point->setX(tmpValidPoint->getX());
                        point->setY(tmpValidPoint->getY());
                        point->setRealX(tmpValidPoint->getRealX());
                        point->setRealY(tmpValidPoint->getRealY());
                        point->setRealA(tmpValidPoint->getRealA());
                        QString tmpNote="sucess:vmark点坐标修改成功，id:";
                        tmpNote+=QString::number(point->getId());
                        ui->textEdit_result->append(tmpNote);

                }
                if(E_POSITION_TYPE_QRCODE==point->getPoseType() && ui->checkBox_qrcodePosition->isChecked())
                {

                        point->setX(tmpValidPoint->getX());
                        point->setY(tmpValidPoint->getY());
                        point->setRealX(tmpValidPoint->getRealX());
                        point->setRealY(tmpValidPoint->getRealY());
                        point->setRealA(tmpValidPoint->getRealA());
                        QString tmpNote="sucess:二维码点坐标修改成功，id:";
                        tmpNote+=QString::number(point->getId());
                        ui->textEdit_result->append(tmpNote);

                }
                if(E_POSITION_TYPE_REFLECT==point->getPoseType() && ui->checkBox_reflectPosition->isChecked())
                {

                        point->setX(tmpValidPoint->getX());
                        point->setY(tmpValidPoint->getY());
                        point->setRealX(tmpValidPoint->getRealX());
                        point->setRealY(tmpValidPoint->getRealY());
                        point->setRealA(tmpValidPoint->getRealA());
                        QString tmpNote="sucess:reflect反光点坐标修改成功，id:";
                        tmpNote+=QString::number(point->getId());
                        ui->textEdit_result->append(tmpNote);

                }
            }
        }


    }
    oneMap->setVersion(oneMap->getVersion()+1);

    emit updateMapShow_signal();//会导致其它元素ｉｄ也变了，所以要全部更新．
    QMessageBox::information(this,QStringLiteral("提示"),"合并完成！");


}
